package com.pescamillam.kidsspanish;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class KidsSpanishActivity extends Activity {
	Context context = KidsSpanishActivity.this;
	private Constants cons = new Constants();
	public Button [] buttons = new Button[4];
	public static final int MENU_ITEM_ABOUT = Menu.FIRST + 3;
	private static final int DIALOG_ABOUT = 0;
	private TextView question;
	private TextView answer;
	private TextView score;
	private TextView fullQuestion;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    	OkListener t = new OkListener("How do you say?",cons,this);
    	t.setAnswerTextView((TextView)findViewById(R.id.textView1));
    	t.setQuestionTextView((TextView)findViewById(R.id.textView2));
    	t.setScoreTextView((TextView)findViewById(R.id.score));
    	int[] btns = {R.id.button1, R.id.button2, R.id.button3, R.id.button4};
    	for (int i = 0; i<4; i++) {
    		buttons[i] = (Button)findViewById(btns[i]);
    	}
    	question = (TextView)findViewById(R.id.textView2);
    	fullQuestion = (TextView)findViewById(R.id.fullQuestion);
    	answer = (TextView)findViewById(R.id.textView1);
    	score = (TextView)findViewById(R.id.score);
    	fullQuestion.setText("How do you say?");
    	score.setText("Score: ");
    	restartOptions();
    }
    

	public void restartOptions() {
		int realOption = (int)Math.floor(Math.random()*cons.values.get("animals").size());
        List <Integer> array = new ArrayList<Integer>();
    	question.setText((String)cons.values.get("animals").values().toArray()[realOption]);
    	answer.setText("");
        for (int i = 1; i <= 4;) {
  		    int possibleIndex = (int)Math.floor(Math.random()*cons.values.get("animals").size());
  		    if (array.indexOf(possibleIndex) == -1 && possibleIndex != realOption) {
  			    Button btn = buttons[i-1];
	  			btn.setText((String)cons.values.get("animals").keySet().toArray()[possibleIndex]);
	  			i++;
	  			OkListener listener = new OkListener("bad",cons,this);
	  			listener.setAnswerTextView(answer);
	  			btn.setOnClickListener(listener);
  		    }
        }
        Button btn = buttons[(int)Math.floor(Math.random()*4)];
		btn.setText((String)cons.values.get("animals").keySet().toArray()[realOption]);
		OkListener listener = new OkListener("good",cons,this);
		listener.setAnswerTextView(answer);
		btn.setOnClickListener(listener);
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	menu.add(0, MENU_ITEM_ABOUT, 2, R.string.about)
			.setIcon(android.R.drawable.ic_menu_info_details);
    	Intent intent = new Intent(null, getIntent().getData());
		intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
		menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
				new ComponentName(this, KidsSpanishActivity.class), null, intent, 0, null);
    	return true;
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
    	LayoutInflater factory = LayoutInflater.from(this);
    	switch (id) {
    		case DIALOG_ABOUT:
    			final View aboutView = factory.inflate(R.layout.about, null);
				TextView versionLabel = (TextView) aboutView.findViewById(R.id.version_label);
				String versionName = AndroidUtils.getAppVersionName(getApplicationContext());
				versionLabel.setText(getString(R.string.version, versionName));
				return new AlertDialog.Builder(this)
						.setTitle(R.string.app_name)
						.setView(aboutView)
						.setPositiveButton("OK", null)
						.create();
    	}
    	return null;
    }
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		switch (item.getItemId()) {
			case MENU_ITEM_ABOUT:
				showDialog(DIALOG_ABOUT);
				return true;
		}
		return super.onOptionsItemSelected(item);
	}
}